package com.ruoyi.framework.factory;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.framework.strategy.AuthenticationStrategy;
import com.ruoyi.framework.strategy.DefaultAuthenticationStrategy;
import com.ruoyi.framework.strategy.WxApiAuthenticationStrategy;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.framework.web.service.WxApiTokenService;

/**
 * 认证工厂实现类
 */
@Component
public class AuthenticationStrategyFactory {
    
    private final WxApiAuthenticationStrategy wxApiStrategy;
    private final DefaultAuthenticationStrategy defaultStrategy;

    @Autowired
    public AuthenticationStrategyFactory(WxApiTokenService apiTokenService, TokenService tokenService) {
        this.wxApiStrategy = new WxApiAuthenticationStrategy(apiTokenService);
        this.defaultStrategy = new DefaultAuthenticationStrategy(tokenService);
    }

    public AuthenticationStrategy createStrategy(HttpServletRequest request) {
        // 把返回哪个策略挪到工厂里面来
        String requestURI = request.getRequestURI();
        if (requestURI.startsWith("/wxapi")) {
            return wxApiStrategy;
        } else {
            return defaultStrategy;
        }
    }
}
